package com.nuc.iblog.controler;

import com.nuc.iblog.entity.*;
import com.nuc.iblog.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Tyranitarx on 2018/1/11.
 *
 * @Description :文章相关
 */
@Controller
@RequestMapping("cl")
@SessionAttributes
public class ClArticleController {
    Logger log = LoggerFactory.getLogger(ClArticleController.class);
    @Autowired
    private ClArticleService clArticleService;
    @Autowired
    private ClUserLoveService userLoveService;
    @Autowired
    private ClUserService userService;
    @Autowired
    private MediaService mediaService;
    @Autowired
    private UserBlogService userBlogService;

    private Article article;
    private List<Comments> comments;
    private Map<String, Object> returnMap;
    //private User user;

    @RequestMapping("/findArticle")
    public String findArticle(int aid, HttpServletRequest request) {
        returnMap = new HashMap<String, Object>();
        article = clArticleService.getArticle(aid);
        comments = article.getComments();
        returnMap.put("Article", article);
        returnMap.put("Comments", comments);
        request.setAttribute("ArticleMap", returnMap);
        return "/cl/article";
    }

    @RequestMapping("jumpto")
    public String jum(){
        return "/cl/jump";
    }

    @ResponseBody
    @RequestMapping("/AriStar")
    public Map<String, Object> loveArticle(int aid, HttpSession httpSession) {
        User user = (User) httpSession.getAttribute("User");
        log.info("获取到的用户"+aid);
        int code=userLoveService.love(user.getUid(), aid);
        if(code==-1){
            returnMap = new HashMap<String, Object>();
            returnMap.put("msg", "dislike");
            return returnMap;
        }else {
            returnMap = new HashMap<String, Object>();
            returnMap.put("msg", "like");
            return returnMap;
        }
    }

    @ResponseBody
    @RequestMapping("/Lovestatus")
    public Map<String,Object> lovestatus(int aid,HttpSession httpSession){
        User user=(User)httpSession.getAttribute("User");
        int code=userLoveService.lovestatus(user.getUid(),aid);
        if(code==-1){
            returnMap = new HashMap<String, Object>();
            returnMap.put("msg", "dislike");
            return returnMap;
        }else {
            returnMap = new HashMap<String, Object>();
            returnMap.put("msg", "like");
            return returnMap;
        }
    }


    private Page<Article> articlePage;

    @RequestMapping("/selfBlog")
    public String selfBlog(HttpServletRequest request, @RequestParam(defaultValue = "0") int pagenum) {
        User user = (User) request.getSession().getAttribute("User");
        UserBlog userBlog=userBlogService.findByuser(user);
        log.info("用户" + user.getUsername() + "查看个人博客");
        if (pagenum < 0)
            pagenum = 0;
        articlePage = clArticleService.getPageArticle(user.getUid(), pagenum,userBlog.getArticlenum());
        log.info(articlePage.getTotalPages() + "");
        if(!articlePage.getContent().isEmpty())
            request.setAttribute("Articles", articlePage.getContent());
        else
            request.setAttribute("Articles", null);
        request.setAttribute("pagenums", articlePage.getTotalPages());
        request.setAttribute("pagenum", pagenum);
        request.setAttribute("music",mediaService.getbackgroundmusic(1,user));
        request.setAttribute("selfBlog",userBlogService.findByuser(user));
        return "cl/selfblog";
    }

    @RequestMapping("/otherBlog")
    public String ohterBlog(HttpServletRequest request,@RequestParam(defaultValue = "0")int pagenum,@RequestParam("uid")int uid){
        User user=userService.getUser(uid);
        UserBlog userBlog=userBlogService.findByuser(user);
        articlePage=clArticleService.getPageArticle(uid,pagenum,userBlog.getArticlenum());
        request.setAttribute("User",user);
        request.setAttribute("Articles", articlePage.getContent());
        request.setAttribute("pagenums", articlePage.getTotalPages());
        request.setAttribute("pagenum", pagenum);
        request.setAttribute("music",mediaService.getbackgroundmusic(1,user));
        request.setAttribute("otherBlog",userBlogService.findByuser(user));
        return "cl/otherblog";
    }


    private ArticlePage articlePage1;

    @RequestMapping("/moreinfo")
    public String moinfo(HttpServletRequest request, int catid, @RequestParam(defaultValue = "0") int pagenum) {
        if (pagenum < 0)
            pagenum = 0;
        log.info(catid + "");
        articlePage1 = clArticleService.getAcademyArticle(catid, pagenum);
        request.setAttribute("pagenums", articlePage1.getTotalpage());
        request.setAttribute("catid", catid);
        request.setAttribute("Info", articlePage1.getContent());
        request.setAttribute("pagenum", pagenum);
        return "cl/moreinfo";
    }

    @RequestMapping("/searchArticle")
    public ModelAndView searchArticle(@RequestParam("input")String input, ModelAndView modelAndView){
        modelAndView.addObject("Articles",clArticleService.searchArticle(input));
        modelAndView.setViewName("/cl/search");
        return modelAndView;
    }
}
